package com.gorkr;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/01/12 21:45
 * 比特位计数
 **/
public class L338 {

    // “i&（i-1）”将整数i的最右边的1变成0
    public int[] countBits(int n) {
        int[] result = new int[n+1];

        for (int i = 0; i <=n ; i++) {
            int num = 0;
            int tmp = i;

            while (tmp!=0){
                tmp=(tmp&(tmp-1));
                num++;
            }
            result[i]=num;
        }

        return result;
    }
    @Test
    public void test(){
        System.out.println(countBits(5));
    }
}
    